﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using BLL;
using Entities;
using Entities.IRepository;
using DAL;
namespace HeThongNhaSach
{
    public partial class frmTimKiemNhanVien : Form
    {
        public frmMain frmChinh;
        public frmTimKiemNhanVien()
        {
            InitializeComponent();
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            GeneralRepository<NhanVien> _HDRepo = new GeneralRepository<NhanVien>();
            List<NhanVien> lsb = _HDRepo.GetAll().ToList();
            if (cbTimTheoMa.Checked)
            {
                lsb = lsb.FindAll(delegate(NhanVien s)
                {
                    return s.MaNhanVien.Contains(txtTimMa.Text.Trim());
                });
            }
            if (cbTimTheoTen.Checked && !string.IsNullOrEmpty(txtTimTen.Text.Trim()))
            {
                lsb = lsb.FindAll(delegate(NhanVien s)
                {
                    return s.HoTen.Contains(txtTimTen.Text.Trim());
                });
            }
            if (cbTimTheoLuong.Checked)
            {
                try
                {
                    float min = float.Parse(txtMin.Text);
                    float max = float.Parse(txtMax.Text);
                    lsb = lsb.FindAll(delegate(NhanVien s)
                    {
                        return s.LuongCoBan >= min && s.LuongCoBan <= max;
                    });
                }
                catch
                {
                    MessageBox.Show("Nhập luong cần tìm");
                    return;
                }
            }
            if (cbTimTheoNgay.Checked)
            {
                try
                {
                    DateTime min = dtMin.Value;
                    DateTime max = dtMax.Value;
                    lsb = lsb.FindAll(delegate(NhanVien s)
                    {
                        return s.NgaySinh >= min && s.NgaySinh <= max;
                    });
                }
                catch
                {
                    MessageBox.Show("Nhập ngày cần tìm");
                    return;
                }
            }
           
            if (lsb.Count <= 0)
            {
                MessageBox.Show("Không tìm thấy nhân viên phù hợp");
            }
            else
            {
                frmChinh.tabControl1.SelectedTab = frmChinh.tabNV;
                frmChinh.dgvNhanVien.DataSource = new List<NhanVien>();
                frmChinh.dgvNhanVien.DataSource = lsb;
                this.Close();
            }
        }

        private void frmTimKiemNhanVien_Load(object sender, EventArgs e)
        {
            txtTimMa.Enabled = false;
            grpTimTheoNgay.Enabled = false;
            grpTimLuong.Enabled = false;
            txtTimTen.Enabled = false;

            btnTimKiem.Enabled = false;
        }

        private void btnhuy_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cbTimTheoMa_CheckedChanged(object sender, EventArgs e)
        {
            if (cbTimTheoMa.Checked)
            {
                txtTimMa.Enabled = true;
                btnTimKiem.Enabled = true;
            }
            else
            {
                txtTimMa.Enabled = false;
                if (cbTimTheoLuong.Checked || cbTimTheoMa.Checked || cbTimTheoTen.Checked || cbTimTheoNgay.Checked) ;
                else
                    btnTimKiem.Enabled = false;
            }
        }

        private void cbTimTheoTen_CheckedChanged(object sender, EventArgs e)
        {
            
            if (cbTimTheoTen.Checked)
            {
                txtTimTen.Enabled = true;
                btnTimKiem.Enabled = true;
            }
            else
            {
                txtTimTen.Enabled = false;
                if (cbTimTheoLuong.Checked || cbTimTheoMa.Checked || cbTimTheoTen.Checked || cbTimTheoNgay.Checked ) ;
                else
                    btnTimKiem.Enabled = false;
            }
        }

        private void cbTimTheoNgay_CheckedChanged(object sender, EventArgs e)
        {
            if (cbTimTheoNgay.Checked)
            {
                grpTimTheoNgay.Enabled = true;
                btnTimKiem.Enabled = true;
            }
            else
            {
                grpTimLuong.Enabled = false;
                if (cbTimTheoLuong.Checked || cbTimTheoMa.Checked || cbTimTheoTen.Checked || cbTimTheoNgay.Checked) ;
                else
                    btnTimKiem.Enabled = false;
            }
        }

        private void cbTimTheoLuong_CheckedChanged(object sender, EventArgs e)
        {
            if (cbTimTheoLuong.Checked)
            {
                grpTimLuong.Enabled = true;
                btnTimKiem.Enabled = true;
            }
            else
            {
                grpTimLuong.Enabled = false;
                if (cbTimTheoLuong.Checked || cbTimTheoMa.Checked || cbTimTheoTen.Checked || cbTimTheoNgay.Checked) ;
                else
                    btnTimKiem.Enabled = false;
            }
        }
    }
}
